package com.onairm.baselibrary.mvp.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bqy on 2018/3/28.
 */

public abstract class BaseRefreshAdapter<D, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {

    private Context context;
    private List<D> list;
    private LayoutInflater inflater;

    private OnItemKeyListener onItemKeyListener;
    private OnItemClickListener mOnItemClick;
    private OnItemLongClickListener longClickListener;
    private OnItemSelectListener mSelectListener;

    public BaseRefreshAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
        this.inflater = LayoutInflater.from(context);
    }

    public Context getContext() {
        return context;
    }

    public List<D> getList() {
        return list;
    }

    public LayoutInflater getInflater() {
        return inflater;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void refreshAdapter(boolean isDownRefresh, List<D> list) {
        if (isDownRefresh) {
            getList().clear();
        }
        int size = getList().size();
        if (list != null) {
            getList().addAll(list);
        }
        //刷新
        notifyItemRangeInserted(size, getList().size());
    }

    protected void setOnItemSelected(final View v, final int position) {
        if (null != mSelectListener) {
            mSelectListener.onItemSelect(v, position);
        }
    }

    public void setOnItemSelectListener(OnItemSelectListener listener) {
        this.mSelectListener = listener;
    }

    public void setOnItemClickListener(OnItemClickListener clickListener) {
        this.mOnItemClick = clickListener;
    }

    public void setOnLongClickListener(OnItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    protected void setOnItemLongClick(final View view, final int position) {
        if (null != longClickListener) {
            longClickListener.onItemLongClick(view, position);
        }
    }

    protected void setOnItemClick(final View view, final int position) {
        if (null != mOnItemClick) {
            mOnItemClick.onItemClick(view, position);
        }
    }

    public void setOnItemKeyListener(OnItemKeyListener onItemKeyListener) {
        this.onItemKeyListener = onItemKeyListener;
    }

    protected boolean setOnItemKey(final View view,int keyCode,KeyEvent keyEvent,int position){
        if(null != onItemKeyListener){
            return onItemKeyListener.onKey(view,keyCode,keyEvent,position);
        }else {
            return false;
        }
    }

    public interface OnItemSelectListener {
        void onItemSelect(View view, int position);
    }

    public interface OnItemClickListener {
        void onItemClick(final View view, final int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(final View view, final int position);
    }

    public interface OnItemKeyListener {
        boolean onKey(View view, int keyCode, KeyEvent keyEvent, int position);
    }
}
